package com.ejoysoft.servlet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Vector;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Ap;
import com.ejoysoft.wlanyz.system.User;
import java.util.Vector;
import java.net.*;

public class FtpInfoServlet extends HttpServlet implements Servlet
{
	private static final long serialVersionUID = 217251451801586160L;
	public FtpInfoServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.err.println("12");
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		 Globa globa = new Globa(); 
		 StringBuffer sbReturn = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
			req.setCharacterEncoding("utf-8");		
			resp.setCharacterEncoding("utf-8");	
			try
			{
				
				User user0 = new User(globa,false);
					Vector<User> vctAd = new Vector<User>();
					vctAd = user0.list(" ",0,0);
					sbReturn.append("<Root>");
					if (vctAd!=null && vctAd.size() > 0)
					{
						for (int i = 0; i < vctAd.size(); i++)
						{
							user0 = vctAd.get(i);
							if (user0!=null)
							{   
								int post= req.getServerPort();
								sbReturn.append(returnUser(user0,post));
							}
						}
					}
					sbReturn.append("</Root>");
				

			} catch (Exception e)
			{
				e.printStackTrace();
			} finally
			{
				// 关闭数据库连接对象
				globa.closeCon();
				//System.out.println(sbReturn.toString());
				resp.getWriter().println(sbReturn.toString());
			}
 
	  }
	


	private StringBuffer returnUser (User user1,int post )
	{   
		Globa globa = new Globa();
		 String ipaddr="";
				StringBuffer sbReturnContent = new StringBuffer();
				if(user1!=null)
				{   try {
					InetAddress inet =InetAddress.getLocalHost();
					ipaddr=inet.getHostAddress();
				   } catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				   }
				    if(ipaddr!=null&&post!=0)
				    {
					sbReturnContent.append("<ftp>");
					sbReturnContent.append("<username>" + user1.getStrUserId() + "</username>");
					sbReturnContent.append("<pwd>" +user1.getStrPWD() + "</pwd>");
					sbReturnContent.append("<ip>" +ipaddr + "</ip>");
					sbReturnContent.append("<port>" +post + "</port>");
					sbReturnContent.append("</ftp>");
					sbReturnContent.append("<taskurl>");
					sbReturnContent.append("<url>" +""+ "</url>");
					sbReturnContent.append("</taskurl>");
					sbReturnContent.append("<pingcount>");
					sbReturnContent.append("<Number>" +""+ "</Number>");
					sbReturnContent.append("</pingcount>");
					sbReturnContent.append("<macurl>");
					sbReturnContent.append("<url>" +""+ "</url>");
					sbReturnContent.append("</macurl>");
				    }
				}
				
				globa.closeCon();
				return sbReturnContent;
			  }
				
				
 		 
		
	}
	
